<?php
class Acl_role_model extends MY_Model
{
	public
		$table = 'acl_roles',
		$prKey = 'id'
	;
	
	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'id' 		=> '',
			'name' 		=> T_('Роль'),
			'description' => T_('Описание'),		
			'order' 	=> T_('Порядок'),		
		);

		$this->rules = array(
			'name' 		=> "trim|strip_tags|required|max_length[64]",
			'description' => "trim|strip_tags|max_length[255]",
			'order' 	=> "trim|strip_tags|required|integer",
		);
	}
	
	function getSelectOptions($id_role = null)
	{
		if (!empty($id_role))
		{
			$this->db->where('id', $id_role);
		}
		$this->db->order_by('order');
		$query = $this->db->get($this->table);
		$options = array();
		
		foreach ($query->result() as $row)
		{
			$options[$row->id] = $row->name; 
		}
		
		return $options;
	}

	function getAll()
	{	
		$sql = 'SELECT * FROM "'.$this->table.'" ORDER BY "order"';  
		
		return $this->db->query($sql)->result(); 
	}	

	function getUserRoles($id_user, $id_group)
	{	
		$sql = 'SELECT id_roles FROM `users_roles` WHERE id_users='.$this->db->escape($id_user).' AND id_group='.$this->db->escape($id_group);
		$query = $this->db->query($sql);
		$data = array();
		
		foreach ($query->result() as $row)
		{
			$data[] = $row->id_roles; 
		}
		
		return $data; 
	}
		
	function search($config = array())
	{
		$query = $this->db->get($this->acl_role_model->table);

		$order 		= $this->input->get_post('order');
		$direction 	= $this->input->get_post('direction');
		$search 	= $this->input->get_post('search');

		// main sql		
		$sql = 'SELECT *';
		$sql .= ' FROM '.$this->table.' r';
		$sql .= ' WHERE 1 = 1';  
			
		if ($search)
		{
			$sql .= ' AND r.name LIKE ' . $this->db->escape('%' . $search . '%'); 
		}
		
		switch ($order)
		{			
			default:
				$sql .= ' ORDER BY r.order';
				$direction = 'DESC';
		}
		
		$sql .= ($direction == 'DESC') ? ' DESC' : ' ASC';
		
		$this->data['list'] = $this->db->query($sql)->result();

		$this->data['search'] = $search; 
	}	
	
	function delete($items_serialized = null)
	{
		$items = explode("&", $items_serialized);
		
		$report = "";
		foreach ($items as $id)
		{	
			$data = $this->acl_role_model->get($id);
			
			$res = $this->db->delete($this->table, array($this->prKey => $id));
			$this->db->delete('account_permissions', array('id_account_type' => $id));
			$this->db->delete('users_roles', array('id_roles' => $id));
			
			if (is_null($res))
			{
				$this->log->writeMessage('error', T_('Error. Роль не удалена.'));
				$report .= ",0"; // don't deleted				
			}				
			else
			{
				$this->log->writeMessage('done', T_('Роль удалена.'));
				$report .= ",1"; // deleted
				
		        $log['id_admins'] = $this->auth->getID();
		        $log['message']   = T_('Роль <span class="green">'). $data['name'] . T_('</span> удалена.');
		        $log['location']  = $_SERVER['REQUEST_URI'];
		        $log['data']      = serialize($data);
		        $this->watchdog_model->insert($log);					
			}				
		}
		
		return substr($report, 1);
	}

	function insert($data)
	{
		if (!empty($data))
		{			
			parent::insert($data);

			if ($this->db->insert_id() > 0)
			{
				$this->log->writeMessage('done', T_('Роль успешно добавлена.'));
				
				$log['id_admins'] = $this->auth->getID();
				$log['message']   = T_('Роль <span class="green">'). $data['name'] . T_('</span> успешно добавлена.');
				$log['location']  = $_SERVER['REQUEST_URI'];
				$this->watchdog_model->insert($log);
				
				return TRUE;
			}
			else
			{
				$this->log->writeMessage('error', T_('Error! Роль не создана.'));
				
				return FALSE;
			}
		}		
	}

	function update($data)
	{
		if (!empty($data))
		{
			$res = parent::update($data);

			$this->log->writeMessage('done', T_('Измененные данные сохранены.'));
			
			$log['id_admins'] = $this->auth->getID();
			$log['message']   = T_('Роль <span class="green">'). $data['name'] . T_('</span> отредактирована.');
			$log['location']  = $_SERVER['REQUEST_URI'];
			$this->watchdog_model->insert($log);			

			return TRUE;			
		}		
	}	
}
?>